// 云函数入口文件
const cloud = require('wx-server-sdk')
const env = 'bbq-6gz7knah1c296b0f'
cloud.init(env)

const db = cloud.database({env})

// 云函数入口函数
exports.main = async (event, context) => {
    await db.collection("comments").add({
      data:event
    })
    await db.collection('articles').doc(event.articleId).update({
      data:{
        commentCounts:db.command.inc(1)
      }
    })
    //返回该文章所有评论
    return db.collection('comments').aggregate().lookup({
      from:'users',
      localField:'commentUserId',
      foreignField:'userId',
      as:'commentWithuser'
    }).match({articleId:event.articleId}).end()
  }

